package com.dycx.food.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.food.converter.GoodsClassConverter;
import com.dycx.food.mapper.GoodsClassFoodMapper;
import com.dycx.food.dto.GoodsClassDTO;
import com.dycx.food.entity.GoodsClassDO;
import com.dycx.food.service.GoodsClassFoodService;
import com.dycx.food.vo.GoodsClassVO;
import com.dycx.system.service.OssFileService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
@AllArgsConstructor
public class GoodsClassFoodImpl extends ServiceImpl<GoodsClassFoodMapper, GoodsClassDO> implements GoodsClassFoodService {

    private OssFileService ossFileService;

    @Override
    public Boolean delete(int goodsClassId) {
        GoodsClassDO oldDO = this.getById(goodsClassId);
        if (!this.removeById(goodsClassId)) return false;

        ossFileService.setDeleted(oldDO.getImage());
        return true;
    }

    /**
     * 添加
     *
     * @param goodsClassDto
     * @return
     */
    @Override
    public Boolean insert(GoodsClassDTO goodsClassDto) {
        GoodsClassDO goodsClassDO = GoodsClassConverter.dtoToEntity(goodsClassDto);
        if (!this.save(goodsClassDO)) return false;

        ossFileService.setUse(goodsClassDO.getImage());
        goodsClassDto.setId(goodsClassDO.getId());
        return true;
    }

    /**
     * 添加/修改
     *
     * @param goodsClassDto
     * @return
     */
    @Override
    public Boolean update(GoodsClassDTO goodsClassDto) {
        GoodsClassDO oldDO = this.getById(goodsClassDto.getId());
        GoodsClassDO goodsClassDO = GoodsClassConverter.dtoToEntity(goodsClassDto);
        if (!this.updateById(goodsClassDO)) return false;

        ossFileService.setDeleted(oldDO.getImage());
        ossFileService.setUse(goodsClassDto.getImage());

        return true;
    }

    /**
     * 获取父角色下的所有角色
     *
     * @param parentId
     * @return
     */
    @Override
    public List<GoodsClassVO> getAllList(Integer parentId, Boolean status) {
        LambdaQueryWrapper<GoodsClassDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsClassDO::getParentId, parentId);

        if (status != null) {
            queryWrapper.eq(GoodsClassDO::getStatus, status);
        }
        queryWrapper.orderByDesc(GoodsClassDO::getSort);

        List<GoodsClassVO> goodsClassVOList = GoodsClassConverter.entityToVoList(this.list(queryWrapper));

        for (GoodsClassVO goodsClassVo : goodsClassVOList) {
            List<GoodsClassVO> childrenList = getAllList(goodsClassVo.getId(), status);
            if (childrenList != null && childrenList.size() > 0) {
                goodsClassVo.setChildren(childrenList);
            }
        }

        return goodsClassVOList;
    }

    /**
     * 更新排序
     *
     * @param goodsClassDto
     * @return
     */
    @Override
    public Boolean updateSort(GoodsClassDTO goodsClassDto) {
        GoodsClassDO goodsClassDO = new GoodsClassDO();
        goodsClassDO.setId(goodsClassDto.getId());
        goodsClassDO.setSort(goodsClassDto.getSort());
        return this.updateById(goodsClassDO);
    }

    /**
     * 更新状态
     *
     * @param goodsClassDto
     * @return
     */
    @Override
    public Boolean updateStatus(GoodsClassDTO goodsClassDto) {
        GoodsClassDO goodsClassDO = new GoodsClassDO();
        goodsClassDO.setId(goodsClassDto.getId());
        goodsClassDO.setStatus(goodsClassDto.getStatus());
        return this.updateById(goodsClassDO);
    }

    /**
     * 获取分类id父路径
     *
     * @param id
     * @return
     */
    @Override
    public List<Integer> getGoodsClassIdLevel(int id) {
        List<Integer> idList = new ArrayList<>();

        GoodsClassDO entity = this.getById(id);

        if (entity.getParentId() != 0) {
            idList.addAll(this.getGoodsClassIdLevel(entity.getParentId()));
        }

        idList.add(id);
        return idList;
    }

    /**
     * 获取分类父路径
     */
    @Override
    public List<GoodsClassVO> getGoodsClassLevel(List<Integer> dataList, int i) {
        List<GoodsClassVO> list = new ArrayList<>();

        GoodsClassDO articleTypeDO = this.getById(dataList.get(i));
        GoodsClassVO articleTypeVO = BeanUtil.toBean(articleTypeDO, GoodsClassVO.class);

        if (i < dataList.size() - 1) {
            i++;
            articleTypeVO.setChildren(getGoodsClassLevel(dataList, i));
        }

        list.add(articleTypeVO);
        return list;
    }

    /**
     * 查询父类下第一级分类
     *
     * @param parentId
     * @return
     */
    @Override
    public List<GoodsClassVO> getListByParentId(Integer parentId) {
        LambdaQueryWrapper<GoodsClassDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsClassDO::getParentId, parentId);
        queryWrapper.orderByDesc(GoodsClassDO::getSort);

        List<GoodsClassDO> typeList = this.list(queryWrapper);

        List<GoodsClassVO> goodsClassVOList = new ArrayList<>();

        typeList.forEach(item -> {
            GoodsClassVO goodsClassVO = GoodsClassConverter.entityToVo(item);

            //是否有子类
            LambdaQueryWrapper<GoodsClassDO> queryCount = new LambdaQueryWrapper<>();
            queryCount.eq(GoodsClassDO::getParentId, item.getId());
            goodsClassVO.setHasChildren(count(queryCount) > 0);

            goodsClassVOList.add(goodsClassVO);
        });

        return goodsClassVOList;
    }


}
